package com.itheima.reggie.common;

import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.entity.User;

public class UserHolder {

    private static final  ThreadLocal threadLocal=new ThreadLocal();

    /**
     * 存值
     * @param user
     */
    public static void setUser(Object user){
        threadLocal.set(user);
    }

    /**
     * 取用户id
     * @return
     */
    public static Long getUserId(){
        Object o = threadLocal.get();
        if(o instanceof User){
            User user = (User) o;
            return user.getId();
        }
        if(o instanceof Employee){
            Employee employee = (Employee) o;
            return employee.getId();
        }
        return null;
    }

    /**
     * 取用户名称
     * @return
     */
    public static String getUserName(){
        Object o = threadLocal.get();
        if(o instanceof User){
            User user = (User) o;
            return user.getName();
        }
        if(o instanceof Employee){
            Employee employee = (Employee) o;
            return employee.getUsername();
        }
        return null;
    }


    /**
     * 取用户对象
     * @return
     */
    public static Object getUser(){
        return threadLocal.get();
    }

    /**
     * 释放数据
     */
    public static void release(){
        threadLocal.remove();
    }
}
